19E - Fairy - CodeForces Solution


dfs and similar divide and conquer dsu *2900

Please click on ads to support us..

C++ Code:

//sim 1 - E
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<pll> vpll;

#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define all(x) (x).begin(), (x).end()

const int mod = 1e9 + 7; //998244353;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int logo = 20;
const int MAXN = 1e6 + 7;
const int off = 1 << logo;
const int trsz = off << 1;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int n, m;
vector<pair<int*, int>> ch;

void rollback(){
	pair<int*, int> cr = ch.back();
	ch.PPB();
	*cr.X = cr.Y;
}

struct union_find{
	int par[MAXN], sz[MAXN], ds[MAXN];
	
	void init(){
		for(int i=1; i<=n; i++) par[i] = i, sz[i] = 1, ds[i] = 0;
	}
	
	int find(int x){
		return x == par[x] ? x : find(par[x]);
	}
	
	int dis(int x){
		int d = 0;
		while(x != par[x]) d ^= ds[x], x = par[x];
		return d;
	}
	
	bool unite(int a, int b){
		int da = dis(a), db = dis(b);
		a = find(a), b = find(b);
		if(a == b) return da != db;
		if(sz[a] < sz[b]) swap(a, b);
		
		ch.PB({&sz[a], sz[a]});
		sz[a] += sz[b];
		
		ch.PB({&par[b], par[b]});
		par[b] = a;
		
		ch.PB({&ds[b], ds[b]});
		ds[b] = (1 ^ da ^ db);
		return 1;
	}
}uf;

vi ret;



struct dynacon{
	vii seg[trsz];
	
	void update(int l, int r, ii vl){
		for(l += off, r += off; l < r; l >>= 1, r >>= 1){
			if(l & 1) seg[l].PB(vl), l++;
			if(r & 1) --r, seg[r].PB(vl);
		}
	}
	
	void dfs(int x, bool gd){
		int psz = ch.size();
		for(auto &y : seg[x]) gd &= uf.unite(y.X, y.Y);
		
		if(x >= off){
			if(x - off < m and gd) ret.PB(x - off + 1);
		}else{
			if(gd){
				dfs(x * 2, gd);
				dfs(x * 2 + 1, gd);
			}
		}
		
		while((int)ch.size() != psz) rollback();
	}
}t;

void solve(){
	cin >> n >> m;
	uf.init();
	for(int i=0; i<m; i++){
		int a, b;
		cin >> a >> b;
		if(i) t.update(0, i, {a, b});
		if(i != m - 1) t.update(i + 1, m, {a, b});
	}
	
	t.dfs(1, 1);
	
	cout << ret.size() << "\n";
	for(auto &x : ret) cout << x << " ";
	cout << "\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case